package edu.bbk.muc.lifetracker.sensormanager;

import android.app.Activity;
import android.util.Log;
import edu.bbk.muc.lifetracker.common.Configuration;
import edu.bbk.muc.lifetracker.common.SensorsToUi;

/**
 * Provides common fields and methods for all Recorder classes, i.e. classes
 * that record events from particular sensor or location manager.
 * 
 * @author irenatyler and romankirillov
 * 
 */
public abstract class Recorder {
  /**
   * Base application activity, needed for creating sensors' listeners. Declared
   * as final as unlikely to change. Derived classes can access it using
   * {@link #getCurrentActivity()} method.
   */
  protected final Activity currentActivity;

  private final SensorsToUi sensorsToUi; //callback receiver for all sensor events

  /**
   * Initialises this class fields.
   * @param currentActivity base application activity
   * @param callback callback receiver for all sensor events
   */
  public Recorder(Activity currentActivity, SensorsToUi callback) {
    this.currentActivity = currentActivity;
    this.sensorsToUi = callback;
  }

  /**
   * Allows sensors to change their behavior based on passed configuration.
   * 
   * @param configuration
   */
  public abstract void acceptNewConfiguration(Configuration configuration);

  /**
   * Performs any start logic for specific {@link Recorder}. Must be re-entrant,
   * i.e. when called on active sensor, shouldn't re-initialize it.
   */
  public abstract void doStart();

  /**
   * Performs any stop logic for specific {@link Recorder}. Must be re-entrant,
   * i.e. when called on non-active sensor, shouldn't attempt to remove it
   * again.
   */
  public abstract void doStop();

  /**
   * Starts {@link Recorder}.
   */
  public void start() {
    Log.i(getTag(), "Starting Recorder");
    doStart();
  }

  
  /**
   * Stops {@link Recorder}. 
   */
  public void stop() {
    Log.i(getTag(), "Stopping Recorder");
    doStop();
  }

  /**
   * Gets base application activity.
   * @return base application activity
   */
  protected Activity getCurrentActivity() {
    return currentActivity;
  }

  /**
   * Gets callback receiver for all sensor events.
   * @return callback receiver for all sensor events
   */
  protected SensorsToUi getSensorsToUi() {
    return sensorsToUi;
  }

  /**
   * Gets tag for this recorder, for example "AccelerationRecorder"
   * @return tag for this recorder
   */
  protected abstract String getTag();

}